package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.pojo.*;
import com.itheima.service.DishService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author wuyiqiang
 * @2022/6/14 16:04
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "/dish",produces = "application/json;charset=utf-8")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增菜品
     * @param dishDto
     * @param request
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpServletRequest request) {
        String username = tokenToUsername(request);
        dishDto.setCreateUser(username);
        dishDto.setUpdateUser(username);

        dishService.save(dishDto);

        return R.success("添加菜品成功");
    }

    /**
     * 分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(@Param("name")String name,@Param("page")Integer page,@Param("pageSize")Integer pageSize){
        R<Page> result = dishService.findByPage(page, pageSize, name);
        return result;
    }

    /**
     * 修改菜品
     * @param dishDto
     * @param request
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto,HttpServletRequest request){
        String username = tokenToUsername(request);
        dishDto.setUpdateUser(username);
        //dishDto.setCategoryId(dishDto.getCategoryId());

        System.out.println(dishDto.getCategoryId()+"==================controller");

        dishService.update(dishDto);
        return R.success("修改菜品成功");
    }

    /**
     * 根据id查询菜品和口味
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") String  id){
        DishDto dishDto = dishService.selectById(id);
        System.out.println("debug================="+id);
        return R.success(dishDto);
    }


    /**
     * 批量停售、起售
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@PathVariable("status") Integer status, @RequestParam("ids") List<String> ids) {
            dishService.status(status,ids);
            return R.success("修改成功");
    }

    /**
     * 利用token获得当前页面用户名
     *
     * @param request
     * @return
     */
    public String tokenToUsername(HttpServletRequest request) {
        //用户id设置
        String token = request.getHeader("Authorization");

        String username = (String) redisTemplate.opsForValue().get(token);

        return username;
    }



    /**
     * 批量或者单独删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam("ids") List<String> ids) {
      dishService.delete(ids);
      return R.success("删除成功");
    }

    /**
     * 下拉查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R dropDown(@Param("categoryId") String categoryId) {
        return dishService.dropDown(categoryId);
    }



}
